package com.cavedroid;


public class DualTiltGameThread extends TiltGameThread {

	public DualTiltGameThread(CaveViewActivity a) {
		super(a);
	}
	
	private static final float HORIZONTAL_TILT_FIELD_SIZE = 20;
	
	private boolean horizontalBaseReadingTaken;
	
	private float horizontalVelocity;
	
	private float horizontalAngle;
	private float horizontalBaseLine;
	
	public float horizontalDirection = 1;
	
	@Override
	protected void readInput() {
		super.readInput();
		
		horizontalAngle = -(float)Math.toDegrees(orientation[1]);

		float tilt = horizontalAngle - horizontalBaseLine;
		
		if (horizontalBaseReadingTaken) {
			if (tilt > 0) {
				horizontalVelocity = horizontalDirection*Math.min(MAX_VERTICAL_VELOCITY*(tilt/HORIZONTAL_TILT_FIELD_SIZE), MAX_VERTICAL_VELOCITY);
			} else {
				horizontalVelocity = horizontalDirection*Math.max(MAX_VERTICAL_VELOCITY*(tilt/HORIZONTAL_TILT_FIELD_SIZE), -MAX_VERTICAL_VELOCITY);
			}
		} else {
			horizontalBaseReadingTaken = true;
			horizontalBaseLine = horizontalAngle;
		}
		
		cave.cameraX += horizontalVelocity;
	}
	
	@Override
	public void onResume() {
		horizontalBaseReadingTaken = false;
		horizontalVelocity = 0;
		horizontalAngle = 0;
		super.onResume();
	}
	
	@Override
	protected void addObstacle() {
		cave.obstacles.newObstacle(cave.loopX, cave.loopY, cave.currentLoopDepth, cave.loopRadius);
	}
	
	@Override
	protected void addCollectible() {
		float angle = (float) (Math.random()*Math.PI*2);
		float distance = (float) (cave.loopRadius*Math.random()*0.8);
		cave.collectible.position(cave.loopX + distance*(float)Math.sin(angle), cave.loopY + distance*(float)Math.cos(angle), cave.currentLoopDepth);
	}
}
